package pers.agony.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.agony.constant.MessageConstant;
import pers.agony.entity.PageResult;
import pers.agony.entity.QueryPageBean;
import pers.agony.entity.Result;
import pers.agony.pojo.CheckGroup;
import pers.agony.pojo.CheckItem;
import pers.agony.service.CheckGroupService;

import java.util.List;

/**
 * @author : Agony
 * @date : Created in 4:00 下午 2021/11/2
 * @Description: checkgroup的控制器
 * @Modified By:
 * @version: : 1.0
 */
@RestController
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @PostMapping("/checkGroup/add.do")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam int[] checkitemIds){

        checkGroupService.add(checkGroup,checkitemIds);

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @PostMapping("/checkGroup/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }

    @GetMapping("/checkGroup/findOne.do")
    public Result findOne(@RequestParam int id){
        CheckGroup checkGroup = checkGroupService.findOne(id);
        if(checkGroup!=null) {
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        }else{
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @GetMapping("/checkGroup/findCheckItemIdsByCheckGroupIds.do")
    public Result findCheckItemIdsByCheckGroupIds(@RequestParam int id){
        List<Integer> list = checkGroupService.findCheckItemIdsByCheckGroupIds(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @PostMapping("/checkGroup/update.do")
    public Result update(@RequestBody CheckGroup checkGroup, @RequestParam int[] checkitemIds){
        checkGroupService.update(checkGroup, checkitemIds);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @PostMapping("/checkGroup/delete.do")
    public Result delete(@RequestParam int id){
        checkGroupService.delete(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/checkGroup/findAll.do")
    public Result findAll(){
        List<CheckItem> list = checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }
}
